<?php

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class Category extends Model
{
    protected $autoWriteTimestamp = true;
    use SoftDelete;

    public function getPageData($page, $size)
    {
        return self::paginate($size, false, [
            'page' => $page
        ]);
    }


    //获取子分类
    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id', 'id');
    }

    //嵌套输出
    public static function nestedCategories($parentId = 0)
    {
        return self::with(['children'])->where('parent_id', $parentId)->select()->each(function ($item) {
            if ($item->children) {
                $item->children = self::nestedCategories($item->id);
            }
        });
    }

}